package cn.tedu.quartz;

import cn.tedu.quartz.SimpleQuartzJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
public class QuartzScheduleJob implements ApplicationRunner {
    @Autowired
    private Scheduler scheduler;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //1.创建JobDetail对象
        JobDetail jobDetail = JobBuilder.newJob(SimpleQuartzJob.class)
                .withIdentity("job1:"+ UUID.randomUUID(), "group1")
                .storeDurably()
                .build();
        //2.创建Trigger对象
        CronTrigger jobTrigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1:"+UUID.randomUUID(), "group1") //唯一标识
                .withSchedule(CronScheduleBuilder.cronSchedule("0/3 * * * * ?"))
                .build();
        //3.启动任务
        scheduler.scheduleJob(jobDetail,jobTrigger);
    }
}
